package service

import (
	"context"
	"fmt"
	"time"

	"gitee.com/hust-ideas/little-genius-backend/config"
	model "gitee.com/hust-ideas/little-genius-backend/dal/db"
	"gitee.com/hust-ideas/little-genius-backend/dal/redis"
	"gitee.com/hust-ideas/little-genius-backend/proto"
	"google.golang.org/grpc"
)

type ServiceContext struct {
	Model  *model.DBModel
	Config *config.Config
	RPC    *grpc.ClientConn
	Redis  *redis.RedisClientInterface
}

func NewServiceContext(c config.Config, m *model.DBModel, redisclient *redis.RedisClientInterface) *ServiceContext {
	//RPC, err := grpc.Dial(c.RPCAddr, grpc.WithInsecure())
	// Redis := redis.InitRedis(c)
	//if err != nil {
	//	panic(err)
	//}
	RPC := &grpc.ClientConn{}
	return &ServiceContext{
		Model:  m,
		Config: &c,
		RPC:    RPC,
		Redis:  redisclient,
	}
}

func (s *ServiceContext) CallQuestionGenService(req *proto.GetQuestionsRequest) (resp *proto.GetQuestionsResponse, err error) {
	client := proto.NewQuestionServiceClient(s.RPC)
	fmt.Println(req)
	fmt.Println(s.Config.RPCAddr)
	ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Config.RPCTimeout)*time.Second)
	defer cancel()
	resp, err = client.GetQuestions(ctx, req)
	if err != nil {
		return nil, err
	}
	return
}
